package com.miao.im.support;

import com.miao.im.domain.UserDo;
import com.miao.im.support.exception.AuthenticationException;

import java.util.Optional;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/4/13 16:51
 * @Description:
 */
public class WebSessionHolder {

    public static final ThreadLocal<UserDo> LOCAL = new ThreadLocal<>();

    public static void set(UserDo userDo) {
        LOCAL.set(userDo);
    }

    public static Optional<UserDo> get() {
        return Optional.ofNullable(LOCAL.get());
    }

    public static Long getUserIdNotNull(){
        return get().map(UserDo::getId)
                .orElseThrow(() -> new AuthenticationException("请重新登录"));
    }

    public static void remove() {
        LOCAL.remove();
    }
}
